package com.example.cardgame_1

import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import androidx.recyclerview.widget.RecyclerView
// 注意：这里导入model1包的Card类（如果model1有自己的Card类）
import com.example.cardgame.model1.Card
import com.example.cardgame.model1.CardMatchingGame // 改为model1包的游戏模型

class CardAdapter1(val game: CardMatchingGame) : RecyclerView.Adapter<CardAdapter1.ViewHolder>() {
    // 内部类和逻辑完全复用原CardAdapter
    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val cardButton: Button = itemView.findViewById(R.id.button)
    }

    var mListener: ((Int)->Unit)?= null
    fun setOnClickListener(l:(Int)->Unit) {
        mListener = l
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_layout, parent, false)
        return ViewHolder(view)
    }

    override fun getItemCount(): Int {
        return game.cards.size
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val card = game.cardAtIndex(position)
        holder.cardButton.isEnabled = !card.isMatched
        if (card.isChosen) {
            holder.cardButton.text = card.toString()
            holder.cardButton.setBackgroundColor(Color.WHITE)
        } else {
            holder.cardButton.text = ""
            holder.cardButton.setBackgroundResource(R.drawable.flag)
        }
        holder.cardButton.setOnClickListener {
            mListener?.invoke(position)
        }
    }
}